<?php

namespace api\modules\test\controllers;

use Yii;
use api\components\Controller;
use common\models\SequenceModel;
use common\models\dynamic\Dynamic;
use common\models\dynamic\DynamicComment;

/**
 * SequenceController for the `test` module
 */
class SequenceController extends Controller {

    public function actionUpdate($id) {
        $query = DynamicComment::findx($id)->orderBy(['id' => SORT_DESC]);
        $model = $query->one();
        $model->attributes = [
            'post' => 'xxx22222',
            'user_id' => rand(1, 10),
        ];
        if (!$model->save()) {
            return $model->errors;
        }
        return $model->attributes;
    }

    public function actionCreate() {
        $skey = rand(1, 100);
        $user = new DynamicComment($skey);
        $user->user_id = 1;
        $user->post = 'xxx';
        $user->save();

        return $user->attributes;
    }

    public function actionRelation() {
        $skey = 1;
        $model = Dynamic::find()->where(['id' => $skey])->one();
        if ($model->comments) {
            foreach ($model->comments as $item) {
                if ($item->user) {
                    //var_dump($item->user->username);
                } else {
                    //echo $item->id;
                }
            }
        }
        return $model->attributes;
    }

}
